Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

428
Views
Manejar el botón Atrás con Angular [Chrome]

Estoy creando un proyecto usando angular, en mi aplicación necesito manejar el evento del botón Atrás. A continuación se muestra el código que funciona bien aparte de un escenario

Código:

 this.location.subscribe(event => { //logout }); history.pushState({}, '');

Este código solo funciona cuando el usuario realiza alguna actividad, si el usuario acaba de aterrizar en la página y hace clic en el botón Atrás, entonces este evento no se captura. Intenté todas las formas pero no funciona.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Una forma sencilla es agregar el evento popstate cada vez que el usuario haga clic en el botón Atrás. Será despedido.

 window.addEventListener('popstate', (event) => { // The popstate event is fired each time when the current history entry changes. // logout or do any thing you like }, false);

Si esto no funciona, puede optar por Angular Router y verificar qué evento se llama.

 constructor(router: Router) { router.events .subscribe((event: NavigationStart) => { if (event.navigationTrigger === 'popstate') { // Perform actions } }); }
over 4 years ago · Santiago Trujillo Report

0

Esto resuelve el problema para mí, capturado incluso cuando no hay actividad por parte del usuario.

 @HostListener('window:popstate', [$event]) onPopState(event: any) { console.log('Event', event); }

PD : probado en Chrome

over 4 years ago · Santiago Trujillo Report

0

Puede intentar anular todo el onpopstate de esta manera

 window.onpopstate = function () { // Your logic should be here... }.bind(this); // This line is to bind the components
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!